package com.rikai.Rikai;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.FrameLayout;

public class MyPub extends com.mopub.mobileads.MoPubView {
  public MyPub(Context context) {
    super(context);
  }
  public MyPub(Context context, AttributeSet attrs) {
    super(context,attrs);
  }
  @Override 
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
      int parentWidth = MeasureSpec.getSize(widthMeasureSpec);
      int parentHeight = MeasureSpec.getSize(heightMeasureSpec);
      if (parentWidth > parentHeight) {
        mAdView.setLayoutParams(new FrameLayout.LayoutParams(parentWidth,parentHeight));
      } else {
        try {
          ((View)getParent()).setOnTouchListener(new OnTouchListener() {
            public boolean onTouch(View v, MotionEvent event) {
            Log.e("Rikai","Ad click");
            MotionEvent ev = MotionEvent.obtain(event);
            int[] location = new int[2];
            v.getLocationOnScreen(location);
            float dx = event.getRawX() - location[0];
            float dy = event.getRawY() - location[1];
            ev.offsetLocation(dy-dx, dx-dy); // swap x and y
            mAdView.onTouchEvent(ev);
            return true;
            }
          });
          mAdView.setLayoutParams(new FrameLayout.LayoutParams(parentHeight,parentWidth));
          AnimationSet s = new AnimationSet(true);
          s.setFillAfter(true);

          RotateAnimation rot = new RotateAnimation(0,90,0,0);
          rot.setDuration(1000);
          rot.setFillAfter(true);
          rot.setInterpolator(new LinearInterpolator());
          s.addAnimation(rot);

          Animation tran = new TranslateAnimation(0,parentWidth,0,0);
          tran.setDuration(1000);
          tran.setFillAfter(true);
          tran.setInterpolator(new LinearInterpolator());
          s.addAnimation(tran);
          startAnimation(s);
        } catch (Throwable t) {
        }
      }
      mAdView.setHorizontalScrollBarEnabled(false);
      mAdView.setVerticalScrollBarEnabled(false);

      super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }


}
